Repository navigation
Cap each WebSocket action frame at one legal action - #39
Merged
Merged
Conversation
A seated player's socket accepted frames up to the ws default of 100 MiB and parsed them in the message handler. The server now sets maxPayload to the compact JSON size of the longest action the engine can mint: PLAY_OPEN with two follow-<safe integer> instance ids and a safe-integer sequence (181 bytes). ws closes a larger frame with 1009 before it emits message, so trailing whitespace after a CONFIRM_HAND does not apply that action. A compact CONFIRM_HAND still applies and is broadcast. The payload error is ignored so the close does not exit the process. The socket wiring now lives in packages/server/src/sockets.ts. Tests: packages/server/tests/action-frame.test.ts.
The seat socket error listener rethrew every error except the maxPayload length error. ws starts closing a socket before it emits error, so throwing skipped close cleanup and killed the process: an invalid-UTF-8 frame (WS_ERR_INVALID_UTF8) or a header declaring a length over 2^53 - 1 (WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH) from one seat took down every room. The listener now never throws; length closes stay quiet and other errors are logged with the room code. Tests: keep the reviewers cases - invalid UTF-8 closes only the offending socket with 1007, the crafted over-length header closes 1009 and the server keeps serving, a legal action padded to exactly the cap applies, and every GameAction variant at its largest legal size fits the cap (checked exhaustively against GameActionSchema so a new variant cannot silently outgrow it).
The upgrade handler stored the room code uppercased but untrimmed, while RoomManager.authenticate trims before lookup, so a seat holder could connect with a newline padded onto their real code. That newline then split the socket error log line added with the frame cap. Trim the code the same way at upgrade so the stored code is the canonical room code, with a regression test that connects with a padded code and checks the logged line.
Master's call work added ADJUST and COVER_CALL to GameActionSchema and gave END_DANCE a callId, so the cap test's maximal entries no longer covered every variant and its exhaustiveness assertion failed on both CI legs. Add both actions at the largest minted instance id, end the dance with call_999 instead of omitting the call, and record the call-id length beside the card-id one. PLAY_OPEN stays the longest legal frame, so the cap itself does not move.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A seated player's socket accepted frames up to the
ws8.21.1 default of 100 MiB and parsed them in the message handler. The socket code moved frompackages/server/src/index.tstopackages/server/src/sockets.ts(attachGameSockets), which setsmaxPayloadto the compact JSON size of the longest action the engine can mint:PLAY_OPENwith twofollow-<safe integer>instance ids and a safe-integer sequence (181 bytes).Review then caught that the socket
errorlistener rethrew every error except the maxPayload one.wsstarts closing a socket before it emitserror, so throwing skipped theclosecleanup and killed the process: one invalid-UTF-8 frame (WS_ERR_INVALID_UTF8) or a 14-byte header declaring a length over 2^53 - 1 (WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH) from one seat took down every room. The listener now never throws: both length closes stay quiet, other errors are logged with the room code, andwsfinishes closing only the offending socket.The cap is now guarded in tests instead of only measured once: every
GameActionvariant at its largest legal size must fit the cap, checked exhaustively againstGameActionSchema.options, so a new or longer variant fails CI rather than 1009-disconnecting legal play.Evidence
Execution evidence
new WebSocketServer({ noServer: true })leftmaxPayloadat 104857600 (installedwsdefault,websocket-server.js). After the first commit, reviewers' failing cases, run as written: cap mutated to 100 bytes still gaveTests 2 passed(test could not detect a cap too small for legalPLAY_OPEN); an invalid-UTF-8 text frame gaveUncaught Exception ... WS_ERR_INVALID_UTF8,Errors 1 error; a crafted 2^64 - 1 declared-length header gaveUncaught Exception RangeError ... WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTH,Errors 1 error.After:
npx vitest run packages/server/tests/action-frame.test.ts- 6 passed, no errors: legalCONFIRM_HANDapplies (sequence 0 → 1), a frame at exactly 181 bytes applies, a frame at 182 bytes closes 1009 with the sequence still 0, invalid UTF-8 closes only that socket with 1007, the crafted over-length header closes 1009 and a second connection to the same server still gets its snapshot, and all 14 variants at maximal size fit the cap. The same cap mutation to 100 bytes now givesTests 1 failed | 5 passed.npx vitest run packages/server/tests/- 29 passed.npm run typecheckpassed for all workspaces;eslintandprettier --checkclean on both changed files. Fullnpm teston this loaded machine (load average 6.2 on 4 cores): 122 passed, 1 failed -packages/engine/tests/properties.test.tstimed out at its 5s default in code this branch does not touch; rerun with--testTimeout=120000gave 2 passed, and CIverifypassednpm teston the previous head.Visual evidence
None: this branch changes no rendered UI files (
packages/server/src/index.ts,packages/server/src/sockets.ts,packages/server/tests/action-frame.test.tsonly).Merge Danger
Door: two-way
Revert either commit independently; no data migration.
Blast Radius: online seat sockets
One oversized or malformed frame now closes only that socket instead of reaching
JSON.parseor crashing the process. Risk is the cap rejecting a legal action: the client sends compactJSON.stringify(packages/client/src/store.ts), the longest legal variant was computed field by field over all 14GameActionSchemavariants, and the exhaustive test above keeps that true.Unaddressed findings
Standards
None. Both Standards findings were addressed: the throwing
errorlistener (packages/server/src/sockets.ts) no longer throws, and the cap's duplication of engine facts (`${role}-${counter}`ids frompackages/engine/src/game.ts,PLAY_OPENlongest inpackages/engine/src/schemas.ts) is now guarded by the exhaustive variant test rather than moved into the engine - the engine still does not own WebSocket frame bytes.Spec
None. Both Spec findings were addressed: the regression test now fails when the cap falls below a legal action (mutation evidence above), and the sibling length error
WS_ERR_UNSUPPORTED_DATA_PAYLOAD_LENGTHno longer takes down the process. The fix follows the Standards finding slightly further than the Spec finding's minimum: all per-socket errors are logged instead of rethrown, becausewshas already started closing that socket before emitting any of them.Security
None unaddressed. One introduced finding was fixed on this branch (9ad0ab6): the seat socket error log line in
packages/server/src/sockets.tsinterpolated the stored room code, which the upgrade handler uppercased but did not trim whileRoomManager.authenticatetrims before lookup - so a seat holder connecting with a newline padded onto their real code split the log line ([clave] seat socket error in room <code>then: Invalid WebSocket frame: invalid UTF-8 sequenceon the next line, captured by the regression test's console spy before the fix). The upgrade handler now trims the code to matchauthenticate, andkeeps a whitespace-padded room code out of the socket error log lineinpackages/server/tests/action-frame.test.tspasses with the exact single-line message (7 passed in that file, 30 inpackages/server/tests/). A fragmentation bypass of the new cap was also probed - two sub-cap fragments reassembling over 181 bytes - and held:wsaccumulates fragment lengths againstmaxPayload, the socket closed 1009, and the action did not apply.Security review outcome
No unaddressed introduced findings.
Review coverage
Both axes read every changed file against
refs/remotes/origin/master:packages/server/src/index.ts,packages/server/src/sockets.ts, andpackages/server/tests/action-frame.test.ts. No changed file was left unread, and no generated output was excluded.Closes #38
Built with muse Muse Code 1.4.25 (1.4.25-R9361.1) · muse-wonder-full-1.0 · max